package com.xiaomaoguai.user.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaomaoguai.user.model.User;
import com.xiaomaoguai.user.service.UserService;
import com.xiaomaoguai.webkit.Constants;
import com.xiaomaoguai.webkit.web.action.BaseController;

/**
 * @Description :
 * @FileName: UserAction.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年5月8日 上午11:08:59
 * @Version:V1.00
 */
@Controller
@RequestMapping(UserController.PATH)
public class UserController extends BaseController {

	protected static final String PATH = "user/";

	@Autowired
	private UserService userService;

	@Override
	protected String getJspPathPrefix() {
		return PATH;
	}

	@RequestMapping(value = "hello", method = RequestMethod.GET)
	public String toPendingList(Model model, HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("hello", "hello world!");
		HttpSession session = request.getSession();
		session.setAttribute(Constants.CURRENT_USER, "zhang");
		Cookie cookie = new Cookie("JSESSIONID", session.getId());
		response.addCookie(cookie);
		System.out.println(System.getProperty("download.path"));
		return getJspPathPrefix("hello");
	}

	@RequestMapping(value = "json", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public List<User> toUserList(Model model, HttpServletRequest request, HttpSession session) {
		User user = new User();
		user.setUserId(1);
		user.setUserName("zhang");
		User user2 = new User();
		user2.setUserId(2);
		user2.setUserName("张");

		User user3 = userService.findById(10);
		System.out.println(user3);

		return Arrays.asList(user, user2);
	}

}
